{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Querying a specdb source Catalog [v1.0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# import\n",
    "from astropy import units as u\n",
    "from astropy.coordinates import SkyCoord\n",
    "\n",
    "import specdb\n",
    "from specdb.specdb import SpecDB\n",
    "from specdb import specdb as spdb_spdb\n",
    "from specdb.cat_utils import flags_to_groups"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Use test file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "db_file = specdb.__path__[0]+'/tests/files/IGMspec_DB_v02_debug.hdf5'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Database is igmspec\n",
      "Created on 2017-Jan-02\n"
     ]
    }
   ],
   "source": [
    "reload(spdb_spdb)\n",
    "sdb = spdb_spdb.SpecDB(db_file=db_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Show a few things"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "&lt;Table length=5&gt;\n",
       "<table id=\"table4625490192\" class=\"table-striped table-bordered table-condensed\">\n",
       "<thead><tr><th>flag_group</th><th>sig_zem</th><th>flag_zem</th><th>RA</th><th>DEC</th><th>STYPE</th><th>zem</th><th>IGM_ID</th></tr></thead>\n",
       "<thead><tr><th>int64</th><th>float64</th><th>str10</th><th>float64</th><th>float64</th><th>str3</th><th>float64</th><th>int64</th></tr></thead>\n",
       "<tr><td>1</td><td>-1.000</td><td>BOSS_PCA</td><td>0.0019</td><td>17.7737</td><td>QSO</td><td>2.308</td><td>0</td></tr>\n",
       "<tr><td>1</td><td>0.002</td><td>BOSS_PCA</td><td>0.0028</td><td>14.9747</td><td>QSO</td><td>2.516</td><td>1</td></tr>\n",
       "<tr><td>1</td><td>0.001</td><td>BOSS_PCA</td><td>0.0041</td><td>4.8298</td><td>QSO</td><td>1.629</td><td>2</td></tr>\n",
       "<tr><td>1</td><td>0.000</td><td>BOSS_PCA</td><td>0.0053</td><td>-2.0333</td><td>QSO</td><td>1.362</td><td>3</td></tr>\n",
       "<tr><td>1</td><td>0.001</td><td>BOSS_PCA</td><td>0.0057</td><td>-1.3250</td><td>QSO</td><td>2.335</td><td>4</td></tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<Table length=5>\n",
       "flag_group sig_zem  flag_zem    RA      DEC    STYPE   zem   IGM_ID\n",
       "  int64    float64   str10   float64  float64   str3 float64 int64 \n",
       "---------- ------- --------- -------- -------- ----- ------- ------\n",
       "         1  -1.000 BOSS_PCA    0.0019  17.7737   QSO   2.308      0\n",
       "         1   0.002 BOSS_PCA    0.0028  14.9747   QSO   2.516      1\n",
       "         1   0.001 BOSS_PCA    0.0041   4.8298   QSO   1.629      2\n",
       "         1   0.000 BOSS_PCA    0.0053  -2.0333   QSO   1.362      3\n",
       "         1   0.001 BOSS_PCA    0.0057  -1.3250   QSO   2.335      4"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdb.cat[0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[u'BOSS_DR12', u'HD-LLS_DR1', u'GGG', u'SDSS_DR7']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdb.groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{u'BOSS_DR12': 1, u'GGG': 16, u'HD-LLS_DR1': 8, u'SDSS_DR7': 2}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdb.group_dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Querying without Coordinates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generate a query dict\n",
    "\n",
    "#### The following query dict will restrict to all sources with the following:\n",
    "\n",
    "    redshift:  3<z<5\n",
    "    STYPE == 'QSO'\n",
    "    In any of the data groups with flag = 2, 4 or 32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "qdict = {'zem': (3.,5.), 'flag_group-BITWISE-OR': [2,4,32], 'STYPE': 'QSO'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Query call\n",
    "    Returns a bool array of rows that match, the subset of the catalog, and the ID values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "matches, sub_cat, IDs = sdb.qcat.query_dict(qdict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False, False, False,  True, False,  True,\n",
       "       False,  True,  True, False,  True,  True,  True,  True, False,  True], dtype=bool)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matches"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "&lt;Table length=3&gt;\n",
       "<table id=\"table4625587152\" class=\"table-striped table-bordered table-condensed\">\n",
       "<thead><tr><th>flag_group</th><th>sig_zem</th><th>flag_zem</th><th>RA</th><th>DEC</th><th>STYPE</th><th>zem</th><th>IGM_ID</th></tr></thead>\n",
       "<thead><tr><th>int64</th><th>float64</th><th>str10</th><th>float64</th><th>float64</th><th>str3</th><th>float64</th><th>int64</th></tr></thead>\n",
       "<tr><td>3</td><td>0.001</td><td>BOSS_PCA</td><td>0.1035</td><td>24.9509</td><td>QSO</td><td>3.205</td><td>123</td></tr>\n",
       "<tr><td>3</td><td>0.002</td><td>BOSS_PCA</td><td>0.1934</td><td>1.2391</td><td>QSO</td><td>3.738</td><td>225</td></tr>\n",
       "<tr><td>8211</td><td>0.003</td><td>BOSS_PCA</td><td>2.8135</td><td>14.7672</td><td>QSO</td><td>4.986</td><td>3244</td></tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<Table length=3>\n",
       "flag_group sig_zem  flag_zem    RA      DEC    STYPE   zem   IGM_ID\n",
       "  int64    float64   str10   float64  float64   str3 float64 int64 \n",
       "---------- ------- --------- -------- -------- ----- ------- ------\n",
       "         3   0.001 BOSS_PCA    0.1035  24.9509   QSO   3.205    123\n",
       "         3   0.002 BOSS_PCA    0.1934   1.2391   QSO   3.738    225\n",
       "      8211   0.003 BOSS_PCA    2.8135  14.7672   QSO   4.986   3244"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_cat[0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  123,   225,  3244, 17656, 22001, 22730, 24295, 32720, 41217])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "IDs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Query around a Position\n",
    "\n",
    "### Input position can take a few forms as str or tuple:\n",
    "\n",
    "    'J124511+144523'\n",
    "    '124511+144523'\n",
    "    'J12:45:11+14:45:23',\n",
    "    ('12:45:11','+14:45:23')\n",
    "    ('12 45 11', +14 45 23)\n",
    "    (123.123, 12.1224) # Assumes ICRS deg\n",
    "    \n",
    "### Radius is an astropy Angle or Quantity\n",
    "    10*u.arcsec"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### An example or two"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Searching for a single source"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your search yielded 1 match[es] within radius=10 arcsec\n"
     ]
    }
   ],
   "source": [
    "matches, sub_cat, IDs = sdb.qcat.query_position('001115.24+144601.9', 10*u.arcsec)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "&lt;Table length=1&gt;\n",
       "<table id=\"table4626152208\" class=\"table-striped table-bordered table-condensed\">\n",
       "<thead><tr><th>flag_group</th><th>sig_zem</th><th>flag_zem</th><th>RA</th><th>DEC</th><th>STYPE</th><th>zem</th><th>IGM_ID</th></tr></thead>\n",
       "<thead><tr><th>int64</th><th>float64</th><th>str10</th><th>float64</th><th>float64</th><th>str3</th><th>float64</th><th>int64</th></tr></thead>\n",
       "<tr><td>8211</td><td>0.003</td><td>BOSS_PCA</td><td>2.8135</td><td>14.7672</td><td>QSO</td><td>4.986</td><td>3244</td></tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<Table length=1>\n",
       "flag_group sig_zem  flag_zem    RA      DEC    STYPE   zem   IGM_ID\n",
       "  int64    float64   str10   float64  float64   str3 float64 int64 \n",
       "---------- ------- --------- -------- -------- ----- ------- ------\n",
       "      8211   0.003 BOSS_PCA    2.8135  14.7672   QSO   4.986   3244"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_cat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Now a wide search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your search yielded 12 match[es] within radius=20 deg\n"
     ]
    }
   ],
   "source": [
    "matches, sub_cat, IDs = sdb.qcat.query_position((2.813500,14.767200), 20*u.deg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "&lt;Table length=12&gt;\n",
       "<table id=\"table4371481232\" class=\"table-striped table-bordered table-condensed\">\n",
       "<thead><tr><th>flag_group</th><th>sig_zem</th><th>flag_zem</th><th>RA</th><th>DEC</th><th>STYPE</th><th>zem</th><th>IGM_ID</th></tr></thead>\n",
       "<thead><tr><th>int64</th><th>float64</th><th>str10</th><th>float64</th><th>float64</th><th>str3</th><th>float64</th><th>int64</th></tr></thead>\n",
       "<tr><td>8211</td><td>0.003</td><td>BOSS_PCA</td><td>2.8135</td><td>14.7672</td><td>QSO</td><td>4.986</td><td>3244</td></tr>\n",
       "<tr><td>1</td><td>0.002</td><td>BOSS_PCA</td><td>0.0028</td><td>14.9747</td><td>QSO</td><td>2.516</td><td>1</td></tr>\n",
       "<tr><td>3</td><td>0.001</td><td>BOSS_PCA</td><td>0.0391</td><td>13.9385</td><td>QSO</td><td>2.257</td><td>46</td></tr>\n",
       "<tr><td>1</td><td>-1.000</td><td>BOSS_PCA</td><td>0.0019</td><td>17.7737</td><td>QSO</td><td>2.308</td><td>0</td></tr>\n",
       "<tr><td>1</td><td>0.001</td><td>BOSS_PCA</td><td>0.0041</td><td>4.8298</td><td>QSO</td><td>1.629</td><td>2</td></tr>\n",
       "<tr><td>3</td><td>0.001</td><td>BOSS_PCA</td><td>0.1035</td><td>24.9509</td><td>QSO</td><td>3.205</td><td>123</td></tr>\n",
       "<tr><td>3</td><td>0.002</td><td>BOSS_PCA</td><td>0.1934</td><td>1.2391</td><td>QSO</td><td>3.738</td><td>225</td></tr>\n",
       "<tr><td>3</td><td>0.000</td><td>BOSS_PCA</td><td>0.1788</td><td>0.9276</td><td>QSO</td><td>0.953</td><td>207</td></tr>\n",
       "<tr><td>1</td><td>0.001</td><td>BOSS_PCA</td><td>0.0057</td><td>-1.3250</td><td>QSO</td><td>2.335</td><td>4</td></tr>\n",
       "<tr><td>1</td><td>0.000</td><td>BOSS_PCA</td><td>0.0053</td><td>-2.0333</td><td>QSO</td><td>1.362</td><td>3</td></tr>\n",
       "<tr><td>15</td><td>0.001</td><td>BOSS_PCA</td><td>20.4835</td><td>14.8066</td><td>QSO</td><td>2.877</td><td>21499</td></tr>\n",
       "<tr><td>27</td><td>0.001</td><td>BOSS_PCA</td><td>16.5802</td><td>0.8065</td><td>QSO</td><td>4.432</td><td>17656</td></tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<Table length=12>\n",
       "flag_group sig_zem  flag_zem    RA      DEC    STYPE   zem   IGM_ID\n",
       "  int64    float64   str10   float64  float64   str3 float64 int64 \n",
       "---------- ------- --------- -------- -------- ----- ------- ------\n",
       "      8211   0.003 BOSS_PCA    2.8135  14.7672   QSO   4.986   3244\n",
       "         1   0.002 BOSS_PCA    0.0028  14.9747   QSO   2.516      1\n",
       "         3   0.001 BOSS_PCA    0.0391  13.9385   QSO   2.257     46\n",
       "         1  -1.000 BOSS_PCA    0.0019  17.7737   QSO   2.308      0\n",
       "         1   0.001 BOSS_PCA    0.0041   4.8298   QSO   1.629      2\n",
       "         3   0.001 BOSS_PCA    0.1035  24.9509   QSO   3.205    123\n",
       "         3   0.002 BOSS_PCA    0.1934   1.2391   QSO   3.738    225\n",
       "         3   0.000 BOSS_PCA    0.1788   0.9276   QSO   0.953    207\n",
       "         1   0.001 BOSS_PCA    0.0057  -1.3250   QSO   2.335      4\n",
       "         1   0.000 BOSS_PCA    0.0053  -2.0333   QSO   1.362      3\n",
       "        15   0.001 BOSS_PCA   20.4835  14.8066   QSO   2.877  21499\n",
       "        27   0.001 BOSS_PCA   16.5802   0.8065   QSO   4.432  17656"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_cat  # Ordered by separation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([u'BOSS_DR12,GGG,SDSS_DR7', u'BOSS_DR12', u'BOSS_DR12,SDSS_DR7',\n",
       "       u'BOSS_DR12', u'BOSS_DR12', u'BOSS_DR12,SDSS_DR7',\n",
       "       u'BOSS_DR12,SDSS_DR7', u'BOSS_DR12,SDSS_DR7', u'BOSS_DR12',\n",
       "       u'BOSS_DR12', u'BOSS_DR12,HD-LLS_DR1,SDSS_DR7',\n",
       "       u'BOSS_DR12,HD-LLS_DR1,GGG,SDSS_DR7'], \n",
       "      dtype='<U33')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flags_to_groups(sub_cat['flag_group'], sdb.group_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Now a wide search restricted on groups (source needs occur in only one of the groups)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your search yielded 7 match[es] within radius=20 deg\n"
     ]
    }
   ],
   "source": [
    "matches, sub_cat, IDs = sdb.qcat.query_position((2.813500,14.767200), 20*u.deg, groups=['SDSS_DR7','GGG'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "&lt;Table length=7&gt;\n",
       "<table id=\"table4626339472\" class=\"table-striped table-bordered table-condensed\">\n",
       "<thead><tr><th>flag_group</th><th>sig_zem</th><th>flag_zem</th><th>RA</th><th>DEC</th><th>STYPE</th><th>zem</th><th>IGM_ID</th></tr></thead>\n",
       "<thead><tr><th>int64</th><th>float64</th><th>str10</th><th>float64</th><th>float64</th><th>str3</th><th>float64</th><th>int64</th></tr></thead>\n",
       "<tr><td>8211</td><td>0.003</td><td>BOSS_PCA</td><td>2.8135</td><td>14.7672</td><td>QSO</td><td>4.986</td><td>3244</td></tr>\n",
       "<tr><td>3</td><td>0.001</td><td>BOSS_PCA</td><td>0.0391</td><td>13.9385</td><td>QSO</td><td>2.257</td><td>46</td></tr>\n",
       "<tr><td>3</td><td>0.001</td><td>BOSS_PCA</td><td>0.1035</td><td>24.9509</td><td>QSO</td><td>3.205</td><td>123</td></tr>\n",
       "<tr><td>3</td><td>0.002</td><td>BOSS_PCA</td><td>0.1934</td><td>1.2391</td><td>QSO</td><td>3.738</td><td>225</td></tr>\n",
       "<tr><td>3</td><td>0.000</td><td>BOSS_PCA</td><td>0.1788</td><td>0.9276</td><td>QSO</td><td>0.953</td><td>207</td></tr>\n",
       "<tr><td>15</td><td>0.001</td><td>BOSS_PCA</td><td>20.4835</td><td>14.8066</td><td>QSO</td><td>2.877</td><td>21499</td></tr>\n",
       "<tr><td>27</td><td>0.001</td><td>BOSS_PCA</td><td>16.5802</td><td>0.8065</td><td>QSO</td><td>4.432</td><td>17656</td></tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<Table length=7>\n",
       "flag_group sig_zem  flag_zem    RA      DEC    STYPE   zem   IGM_ID\n",
       "  int64    float64   str10   float64  float64   str3 float64 int64 \n",
       "---------- ------- --------- -------- -------- ----- ------- ------\n",
       "      8211   0.003 BOSS_PCA    2.8135  14.7672   QSO   4.986   3244\n",
       "         3   0.001 BOSS_PCA    0.0391  13.9385   QSO   2.257     46\n",
       "         3   0.001 BOSS_PCA    0.1035  24.9509   QSO   3.205    123\n",
       "         3   0.002 BOSS_PCA    0.1934   1.2391   QSO   3.738    225\n",
       "         3   0.000 BOSS_PCA    0.1788   0.9276   QSO   0.953    207\n",
       "        15   0.001 BOSS_PCA   20.4835  14.8066   QSO   2.877  21499\n",
       "        27   0.001 BOSS_PCA   16.5802   0.8065   QSO   4.432  17656"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_cat # Also ordered by separation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Now a wide search restricted on groups (source needs to occur in all of the groups)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your search yielded 2 match[es] within radius=20 deg\n"
     ]
    }
   ],
   "source": [
    "matches, sub_cat, IDs = sdb.qcat.query_position('001115.24+144601.9', 20*u.deg, \n",
    "                                                groups=['SDSS_DR7','GGG'], in_all_groups=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Now a wide search restricted by a query_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your search yielded 7 match[es] within radius=20 deg\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "&lt;Table length=7&gt;\n",
       "<table id=\"table4626440400\" class=\"table-striped table-bordered table-condensed\">\n",
       "<thead><tr><th>flag_group</th><th>sig_zem</th><th>flag_zem</th><th>RA</th><th>DEC</th><th>STYPE</th><th>zem</th><th>IGM_ID</th></tr></thead>\n",
       "<thead><tr><th>int64</th><th>float64</th><th>str10</th><th>float64</th><th>float64</th><th>str3</th><th>float64</th><th>int64</th></tr></thead>\n",
       "<tr><td>1</td><td>0.002</td><td>BOSS_PCA</td><td>0.0028</td><td>14.9747</td><td>QSO</td><td>2.516</td><td>1</td></tr>\n",
       "<tr><td>3</td><td>0.001</td><td>BOSS_PCA</td><td>0.0391</td><td>13.9385</td><td>QSO</td><td>2.257</td><td>46</td></tr>\n",
       "<tr><td>1</td><td>-1.000</td><td>BOSS_PCA</td><td>0.0019</td><td>17.7737</td><td>QSO</td><td>2.308</td><td>0</td></tr>\n",
       "<tr><td>1</td><td>0.001</td><td>BOSS_PCA</td><td>0.0041</td><td>4.8298</td><td>QSO</td><td>1.629</td><td>2</td></tr>\n",
       "<tr><td>1</td><td>0.001</td><td>BOSS_PCA</td><td>0.0057</td><td>-1.3250</td><td>QSO</td><td>2.335</td><td>4</td></tr>\n",
       "<tr><td>1</td><td>0.000</td><td>BOSS_PCA</td><td>0.0053</td><td>-2.0333</td><td>QSO</td><td>1.362</td><td>3</td></tr>\n",
       "<tr><td>15</td><td>0.001</td><td>BOSS_PCA</td><td>20.4835</td><td>14.8066</td><td>QSO</td><td>2.877</td><td>21499</td></tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<Table length=7>\n",
       "flag_group sig_zem  flag_zem    RA      DEC    STYPE   zem   IGM_ID\n",
       "  int64    float64   str10   float64  float64   str3 float64 int64 \n",
       "---------- ------- --------- -------- -------- ----- ------- ------\n",
       "         1   0.002 BOSS_PCA    0.0028  14.9747   QSO   2.516      1\n",
       "         3   0.001 BOSS_PCA    0.0391  13.9385   QSO   2.257     46\n",
       "         1  -1.000 BOSS_PCA    0.0019  17.7737   QSO   2.308      0\n",
       "         1   0.001 BOSS_PCA    0.0041   4.8298   QSO   1.629      2\n",
       "         1   0.001 BOSS_PCA    0.0057  -1.3250   QSO   2.335      4\n",
       "         1   0.000 BOSS_PCA    0.0053  -2.0333   QSO   1.362      3\n",
       "        15   0.001 BOSS_PCA   20.4835  14.8066   QSO   2.877  21499"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qdict = dict(zem=(1.0, 3.))\n",
    "matches, sub_cat, IDs = sdb.qcat.query_position('001115.24+144601.9', 20*u.deg, query_dict=qdict)\n",
    "sub_cat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Query with a set of coordinates\n",
    "    Input is an astropy.coordinate.SkyCoord object, presumably an array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Test data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "coords = SkyCoord(ra=[0.0028,0.0019], dec=[14.9747,17.7737], unit='deg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Query (successful matches)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your search yielded 2 matches from 2 input coordinates\n"
     ]
    }
   ],
   "source": [
    "matches, subcat, IDs = sdb.qcat.query_coords(coords)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "&lt;Table length=2&gt;\n",
       "<table id=\"table4626412432\" class=\"table-striped table-bordered table-condensed\">\n",
       "<thead><tr><th>flag_group</th><th>sig_zem</th><th>flag_zem</th><th>RA</th><th>DEC</th><th>STYPE</th><th>zem</th><th>IGM_ID</th></tr></thead>\n",
       "<thead><tr><th>int64</th><th>float64</th><th>str10</th><th>float64</th><th>float64</th><th>str3</th><th>float64</th><th>int64</th></tr></thead>\n",
       "<tr><td>1</td><td>0.00152614617889</td><td>BOSS_PCA</td><td>0.00275643009297</td><td>14.9746754937</td><td>QSO</td><td>2.5158747041</td><td>1</td></tr>\n",
       "<tr><td>1</td><td>-1.0</td><td>BOSS_PCA</td><td>0.00189828518376</td><td>17.7737391299</td><td>QSO</td><td>2.30763868197</td><td>0</td></tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<Table length=2>\n",
       "flag_group     sig_zem       flag_zem ... STYPE      zem      IGM_ID\n",
       "  int64        float64        str10   ...  str3    float64    int64 \n",
       "---------- ---------------- --------- ... ----- ------------- ------\n",
       "         1 0.00152614617889 BOSS_PCA  ...   QSO  2.5158747041      1\n",
       "         1             -1.0 BOSS_PCA  ...   QSO 2.30763868197      0"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Check output\n",
    "subcat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Query (one fail)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your search yielded 1 matches from 2 input coordinates\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "&lt;Table length=2&gt;\n",
       "<table id=\"table4626413200\" class=\"table-striped table-bordered table-condensed\">\n",
       "<thead><tr><th>flag_group</th><th>sig_zem</th><th>flag_zem</th><th>RA</th><th>DEC</th><th>STYPE</th><th>zem</th><th>IGM_ID</th></tr></thead>\n",
       "<thead><tr><th>int64</th><th>float64</th><th>str10</th><th>float64</th><th>float64</th><th>str3</th><th>float64</th><th>int64</th></tr></thead>\n",
       "<tr><td>0</td><td>0.0</td><td></td><td>0.0</td><td>0.0</td><td></td><td>0.0</td><td>-1</td></tr>\n",
       "<tr><td>1</td><td>-1.0</td><td>BOSS_PCA</td><td>0.00189828518376</td><td>17.7737391299</td><td>QSO</td><td>2.30763868197</td><td>0</td></tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<Table length=2>\n",
       "flag_group sig_zem  flag_zem        RA        ... STYPE      zem      IGM_ID\n",
       "  int64    float64   str10       float64      ...  str3    float64    int64 \n",
       "---------- ------- --------- ---------------- ... ----- ------------- ------\n",
       "         0     0.0                        0.0 ...                 0.0     -1\n",
       "         1    -1.0 BOSS_PCA  0.00189828518376 ...   QSO 2.30763868197      0"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "coords = SkyCoord(ra=[0.0028,0.0019], dec=[-14.9747,17.7737], unit='deg')\n",
    "matches, subcat, IDs = sdb.qcat.query_coords(coords)\n",
    "subcat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False,  True], dtype=bool)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matches"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Query with qdict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your search yielded 1 matches from 2 input coordinates\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "&lt;Table length=2&gt;\n",
       "<table id=\"table4534232656\" class=\"table-striped table-bordered table-condensed\">\n",
       "<thead><tr><th>flag_group</th><th>sig_zem</th><th>flag_zem</th><th>RA</th><th>DEC</th><th>STYPE</th><th>zem</th><th>IGM_ID</th></tr></thead>\n",
       "<thead><tr><th>int64</th><th>float64</th><th>str10</th><th>float64</th><th>float64</th><th>str3</th><th>float64</th><th>int64</th></tr></thead>\n",
       "<tr><td>0</td><td>0.0</td><td></td><td>0.0</td><td>0.0</td><td></td><td>0.0</td><td>-2</td></tr>\n",
       "<tr><td>1</td><td>-1.0</td><td>BOSS_PCA</td><td>0.00189828518376</td><td>17.7737391299</td><td>QSO</td><td>2.30763868197</td><td>0</td></tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<Table length=2>\n",
       "flag_group sig_zem  flag_zem        RA        ... STYPE      zem      IGM_ID\n",
       "  int64    float64   str10       float64      ...  str3    float64    int64 \n",
       "---------- ------- --------- ---------------- ... ----- ------------- ------\n",
       "         0     0.0                        0.0 ...                 0.0     -2\n",
       "         1    -1.0 BOSS_PCA  0.00189828518376 ...   QSO 2.30763868197      0"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "coords = SkyCoord(ra=[0.0028,0.0019], dec=[14.9747,17.7737], unit='deg')\n",
    "qdict = dict(zem=(1.0, 2.5))\n",
    "matches, subcat, IDs = sdb.qcat.query_coords(coords, query_dict=qdict)\n",
    "subcat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-2,  0])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "IDs  # -2 means there is a source at that coordinate but the other criteria is not matched"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
